package hos.permission;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import androidx.annotation.NonNull;

/**
 * <p>Title: PermissionSettings </p>
 * <p>Description: 权限设置意图类 </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023-11-30 22:41
 */
public class PermissionSettings {
    /**
     * Build.MANUFACTURER
     */
    @NonNull
    protected static final String MANUFACTURER_HUAWEI = "HUAWEI";//华为
    @NonNull
    protected static final String MANUFACTURER_MEIZU = "Meizu";//魅族
    @NonNull
    protected static final String MANUFACTURER_XIAOMI = "Xiaomi";//小米
    @NonNull
    protected static final String MANUFACTURER_SONY = "Sony";//索尼
    @NonNull
    protected static final String MANUFACTURER_OPPO = "OPPO";//oppo
    @NonNull
    protected static final String MANUFACTURER_LG = "LG";
    @NonNull
    protected static final String MANUFACTURER_VIVO = "vivo";//vivo
    @NonNull
    protected static final String MANUFACTURER_SAMSUNG = "samsung";//三星
    @NonNull
    protected static final String MANUFACTURER_ZTE = "ZTE";//中兴
    @NonNull
    protected static final String MANUFACTURER_YULONG = "YuLong";//酷派
    @NonNull
    protected static final String MANUFACTURER_LENOVO = "LENOVO";//联想

    protected static final int PERMISSION_STORAGE_SETTING = 952;

    protected static final int SYSTEM_SETTING = 953;

    /**
     * 跳转到应用设置界面
     *
     * @param activity 上下文
     * @return
     */
    @NonNull
    public static Intent getSetting(@NonNull Context activity) {
        return getSetting(activity, false);
    }

    /**
     * 跳转到应用设置界面
     *
     * @param activity 上下文
     * @return
     */
    @NonNull
    public static Intent getSetting(@NonNull Context activity, boolean isNew) {
        Intent intent;
        switch (Build.MANUFACTURER) {
            case MANUFACTURER_HUAWEI:
                intent = getHuaweiSetting(activity, isNew);
                break;
            case MANUFACTURER_MEIZU:
                intent = getMeizuSetting(activity, isNew);
                break;
            case MANUFACTURER_XIAOMI:
                intent = getXiaomiSetting(activity, isNew);
                break;
            case MANUFACTURER_SONY:
                intent = getSonySetting(activity, isNew);
                break;
            case MANUFACTURER_OPPO:
                intent = getOPPOSetting(activity, isNew);
                break;
            case MANUFACTURER_LG:
                intent = getLGSetting(activity, isNew);
                break;
            default:
                Log.e("startSetting", "目前暂不支持此系统");
                intent = getAppSetting(activity, isNew);
        }
        return intent;
    }

    /**
     * 跳转到Huawei权限配置界面
     *
     * @param activity 上下文
     */
    @NonNull
    private static Intent getHuaweiSetting(@NonNull Context activity, boolean isNew) {
        Intent intent = getIntent("com.meizu.safe.security.SHOW_APPSEC", isNew);
        intent.putExtra("packageName", activity.getPackageName());
        ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");
        intent.setComponent(comp);
        return intent;
    }

    /**
     * 跳转到Meizu权限配置界面
     *
     * @param activity 上下文
     */
    @NonNull
    private static Intent getMeizuSetting(@NonNull Context activity, boolean isNew) {
        Intent intent = getIntent("com.meizu.safe.security.SHOW_APPSEC", isNew);
        intent.putExtra("packageName", activity.getPackageName());
        return intent;
    }

    /**
     * 跳转到Xiaomi权限配置界面
     *
     * @param activity 上下文
     */
    @NonNull
    private static Intent getXiaomiSetting(@NonNull Context activity, boolean isNew) {
        Intent intent = getIntent("miui.intent.action.APP_PERM_EDITOR", isNew);
        intent.putExtra("extra_pkgname", activity.getPackageName());
        ComponentName componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
        intent.setComponent(componentName);
        return intent;
    }

    /**
     * 跳转到Sony权限配置界面
     *
     * @param activity 上下文
     */
    @NonNull
    private static Intent getSonySetting(@NonNull Context activity, boolean isNew) {
        Intent intent = getIntent(Intent.ACTION_MAIN, isNew);
        intent.putExtra("packageName", activity.getPackageName());
        ComponentName comp = new ComponentName("com.sonymobile.cta", "com.sonymobile.cta.SomcCTAMainActivity");
        intent.setComponent(comp);
        return intent;
    }

    /**
     * 跳转到OPPO权限配置界面
     *
     * @param activity 上下文
     */
    @NonNull
    private static Intent getOPPOSetting(@NonNull Context activity, boolean isNew) {
        Intent intent = getIntent(Intent.ACTION_MAIN, isNew);
        intent.putExtra("packageName", activity.getPackageName());
        ComponentName comp = new ComponentName("com.coloros.securitypermission", "com.coloros.securitypermission.permission.PermissionAppAllPermissionActivity");//R11t 7.1.1 os-v3.2
        intent.setComponent(comp);
        return intent;
    }

    /**
     * 跳转到LG权限配置界面
     *
     * @param activity 上下文
     */
    @NonNull
    private static Intent getLGSetting(@NonNull Context activity, boolean isNew) {
        Intent intent = getIntent(Intent.ACTION_MAIN, isNew);
        intent.putExtra("packageName", activity.getPackageName());
        ComponentName comp = new ComponentName("com.android.settings",
                "com.android.settings.Settings$AccessLockSummaryActivity");
        intent.setComponent(comp);
        return intent;
    }

    /**
     * 默认打开应用详细页
     *
     * @return
     */
    @NonNull
    public static Intent getAppSetting(@NonNull Context context) {
        return getAppSetting(context, false);
    }

    /**
     * 默认打开应用详细页
     *
     * @return
     */
    @NonNull
    public static Intent getAppSetting(@NonNull Context context, boolean isNew) {
        Intent intent = getIntent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, isNew);
        Uri uri = Uri.fromParts("package", context.getPackageName(), null);
        intent.setData(uri);
        return intent;
    }

    /**
     * 系统设置界面
     */
    @NonNull
    public static Intent getSystemConfig(boolean isNew) {
        return getIntent(Settings.ACTION_SETTINGS, isNew);
    }

    /**
     * 获取意图
     *
     * @param action
     * @return
     */
    @NonNull
    public static Intent getIntent(String action) {
        return getIntent(action, false);
    }

    /**
     * 获取意图
     *
     * @param action
     * @return
     */
    @NonNull
    public static Intent getIntent(@NonNull String action, boolean isNew) {
        Intent intent = new Intent(action);
        if (isNew) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        return intent;
    }

    @NonNull
    public static Intent getAppNotification(@NonNull Context context) {
        return getAppNotification(context, false);
    }

    @NonNull
    public static Intent getAppNotification(@NonNull Context context, boolean isNew) {
        Intent intent = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            intent = getIntent(Settings.ACTION_APP_NOTIFICATION_SETTINGS, isNew);
            intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
        } else {
            intent = getSetting(context, isNew);
        }
        return intent;
    }

    @NonNull
    public static Intent getAppInstall(@NonNull Context context) {
        return getAppInstall(context, false);
    }

    @NonNull
    public static Intent getAppInstall(@NonNull Context context, boolean isNew) {
        Intent intent = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            intent = getIntent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, isNew);
        } else {
            intent = getSetting(context, isNew);
        }
        intent.setData(Uri.parse("package:" + context.getPackageName()));
        return intent;
    }

    @NonNull
    public static Intent getAppStorage(@NonNull Context context) {
        return getAppStorage(context, false);
    }

    @NonNull
    public static Intent getAppStorage(@NonNull Context context, boolean isNew) {
        Intent intent;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            intent = getIntent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, isNew);
        } else {
            intent = getSetting(context, isNew);
        }
        intent.setData(Uri.parse("package:" + context.getPackageName()));
        if (intent.resolveActivity(context.getPackageManager()) == null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                intent = getIntent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION, isNew);
            }
        }
        return intent;
    }

    @NonNull
    public static Intent getAppWriteSettings(@NonNull Context context) {
        return getAppWriteSettings(context, false);
    }

    @NonNull
    public static Intent getAppWriteSettings(@NonNull Context context, boolean isNew) {
        Intent intent;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            intent = getIntent(Settings.ACTION_MANAGE_WRITE_SETTINGS, isNew);
        } else {
            intent = getSetting(context, isNew);
        }
        intent.setData(Uri.parse("package:" + context.getPackageName()));
        return intent;
    }

    @NonNull
    public static Intent getAppSystemAlert(@NonNull Context context) {
        return getAppSystemAlert(context, false);
    }

    @NonNull
    public static Intent getAppSystemAlert(@NonNull Context context, boolean isNew) {
        Intent intent;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            intent = getIntent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, isNew);
        } else {
            intent = getSetting(context, isNew);
        }
        intent.setData(Uri.parse("package:" + context.getPackageName()));
        return intent;
    }


    /**
     * 跳转到定位设置页面
     */
    public static Intent getLocationSetting() {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        return intent;
    }

}
